<?php

require_once __DIR__ . '/inc/silex.phar';

$app = new Silex\Application();

$app->get('/json', function () use ($app) {
            $file = file_get_contents('data.json');
            $storedArr = unserialize(urldecode($file));

            $groups = array();
            $crops = array();
            $rows = array();

            foreach ($storedArr as $group) {
                $groups['group_name'] = key($storedArr);
                $groups['data'] = $group;
                
                foreach ($groups['data'] as $data) {
                    $crops['crop_name'] = key($data);
                    
                    foreach ($data as $crop) {
                        $crops['price'] = $crop['price'];
                        $crops['price_effective_datetime'] = $crop['effective_datetime'];
                    }
                    
                    $rows[] = array($groups['group_name'], $crops['crop_name'], $crops['price'], $crops['price_effective_datetime']);
                }
            }

            $json = array('aaData' => $rows);
            $json = json_encode($json);
            
            echo $json;
        });

$app->get('/data/{data}', function ($data) use ($app) {
            $arr = json_decode(base64_decode(urldecode($data)), true);

            $file = file_get_contents('data.json');
            if (empty($file)) {
                $arr = serialize($arr);
                $arr = urlencode($arr);
                file_put_contents('data.json', $arr);
                $file = file_get_contents('data.json');
            }

            $storedArr = unserialize(urldecode($file));

            /**
             * Merges any number of arrays / parameters recursively, replacing 
             * entries with string keys with values from latter arrays.
             */
            $array_merge_recursive_distinct = function() use (&$array_merge_recursive_distinct) {
                        $arrays = func_get_args();
                        $base = array_shift($arrays);
                        if (!is_array($base))
                            $base = empty($base) ? array() : array($base);
                        foreach ($arrays as $append) {
                            if (!is_array($append))
                                $append = array($append);
                            foreach ($append as $key => $value) {
                                if (!array_key_exists($key, $base) and !is_numeric($key)) {
                                    $base[$key] = $append[$key];
                                    continue;
                                }
                                if (is_array($value) or is_array($base[$key])) {
                                    $base[$key] = $array_merge_recursive_distinct($base[$key], $append[$key]);
                                } else if (is_numeric($key)) {
                                    if (!in_array($value, $base))
                                        $base[] = $value;
                                } else {
                                    $base[$key] = $value;
                                }
                            }
                        }
                        return $base;
                    };

            $newArr = $array_merge_recursive_distinct($arr, $storedArr);
            $newArr = serialize($newArr);
            $newArr = urlencode($newArr);
            file_put_contents('data.json', $newArr);
        });

$app->run();
?>